<?php
// 应用公共文件
/*
 * 打印 输出 断点
 */
function logend($data, $iLogLevel = 0)
{
    if (3 <=  $iLogLevel) {
        var_dump("[ERROR] ");
    } else if (1 <= $iLogLevel) {
        var_dump("[WARN] ");
    } else {
        var_dump("[INFO] ");
    }
    var_dump($data);
    echo "\n<br>";
    exit();
}

function encode($code){
    return base64_encode(md5($code));
}

function error($message, $data = [], $code=400)
{
    $response = json([
        'status' => false,
        'code' => intval($code),
        'msg' => trim($message),
        'data' => $data,
    ]);
//    \app\elk::logStash($data,'resp','new_band_error');
    throw new \think\exception\HttpResponseException($response);
}

function fail($message, $data = [], $code=300)
{
    $response = json([
        'status' => false,
        'code' => intval($code),
        'msg' => trim($message),
        'data' => $data,
    ]);
//    \app\elk::logStash($data,'resp','new_band_fail');
    throw new \think\exception\HttpResponseException($response);
}

function success($data = [], $msg = '执行成功')
{
    $response = json([
        'status' => true,
        'code' => 200,
        'msg' => $msg,
        'data' => $data,
    ]);
    //\app\elk::logStash($data,'resp','new_band_success');
    throw new \think\exception\HttpResponseException($response);
}

/*
 * 获取文件全路径
 */
function fileFullPath($key)
{
    return config('uploader.qiniu.public.domain').$key;
//    return \app\service\QiniuService::getFullPath($key);
}

/*
 * 百度地图经纬度转化腾讯
 */
function bdMap_to_txMap($lat,$lng)
{
    $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    $x = $lng - 0.0065;
    $y = $lat - 0.006;
    $z = sqrt($x * $x + $y * $y) - 0.00002 * sin($y * $x_pi);
    $theta = atan2($y, $x) - 0.000003 * cos($x * $x_pi);
    $lng = $z * cos($theta);
    $lat = $z * sin($theta);
    return array('lng'=>$lng,'lat'=>$lat);
}
/*
 * 验证 职位
 */
function checkPosition($userPosition,$position)
{
    $userPosition = explode(',',$userPosition);
    return in_array($position,$userPosition);
}

function position($position)
{
    $userPosition = explode(',',config('user')['position']);
    return in_array($position,$userPosition);
}
//验证手机号
function checkNormalPhone($phone){

    $pregNormalPhone = "/^1[3456789]{1}\d{9}$/";
    if (!preg_match($pregNormalPhone, $phone)) {
        return false;
    }
    return true;
}
